package com.dhcc.sds.custom.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.Bean2MapUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.entity.SdsIndexHeaderEntity;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.warpper.SdsIndexHeaderWarpper;
import com.dhcc.sds.custom.dao.SdsCusIndexHeaderMapper;
import com.dhcc.sds.custom.entity.SdsCusIndexHeaderEntity;
import com.dhcc.sds.custom.service.ISdsCusIndexHeaderService;
import com.dhcc.sds.standard.service.ISdsStandDataSourceService;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;

/**
 * 自定义病种指标统计配置服务层接口实现
 *
 * @ClassName: SdsCusIndexHeaderServiceImpl
 * @Description: TODO
 * @author yhw
 * @date 2024-07-01 11:14:53
 */
@Service("sdsCusIndexHeaderService")
public class SdsCusIndexHeaderServiceImpl extends BaseServiceImpl<SdsCusIndexHeaderMapper, SdsCusIndexHeaderEntity> implements ISdsCusIndexHeaderService {
    private final String PREFIX = "/backend/sds/sdscusindexheader/";

    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private ISdsStandDataSourceService standDataSourceService;
    @Autowired
    private ISdsStandDicService sdsStandDicService;


    @Override
    public String index() {
        return PREFIX + "sdscusindexheader.html";
    }

    @Override
    public String toAdd(Long entityId, Integer indexType, Model model) {
        model.addAttribute("entityId", entityId);
        model.addAttribute("indexType", indexType);
        return PREFIX + "sdscusindexheader_add.html";
    }

    @Override
    public String toUpdate(Long cusIndexHeaderId, Model model) {
        if (CommonUtil.isEmpty(cusIndexHeaderId)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsCusIndexHeaderEntity sdsCusIndexHeaderEntity = selectById(cusIndexHeaderId);

        // 获取父级菜单的id
        SdsCusIndexHeaderEntity pIndexHeader = selectById(sdsCusIndexHeaderEntity.getParentId());
        String parentName = "";

        // 如果父级是顶级菜单
        if (pIndexHeader == null) {
            sdsCusIndexHeaderEntity.setParentId(Const.TREE_ROOT_ID);
        } else {
            // 设置父级菜单的code为父级菜单的id
            sdsCusIndexHeaderEntity.setParentId(pIndexHeader.getId());
            parentName = pIndexHeader.getTitle();
        }

        Map<String, Object> cusIndexHeaderMap = Bean2MapUtil.toMap(sdsCusIndexHeaderEntity);
        cusIndexHeaderMap.put("parentName", parentName);
        model.addAttribute("cusIndexHeaderMap", cusIndexHeaderMap);
        return PREFIX + "sdscusindexheader_edit.html";
    }

    @Override
    public String toCompare(Long cusIndexHeaderId, Model model) {
        if (CommonUtil.isEmpty(cusIndexHeaderId)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsCusIndexHeaderEntity sdsCusIndexHeaderEntity = selectById(cusIndexHeaderId);
        Map<String, Object> cusIndexHeaderMap = Bean2MapUtil.toMap(sdsCusIndexHeaderEntity);
        //model.addAttribute("formItems",sdsCommonService.findFormItems(1l));
        model.addAttribute("cusIndexHeaderMap", cusIndexHeaderMap);
        JSONArray dicDescMap = sdsStandDicService.getDicDescMap();
        JSONArray dataDescMap = standDataSourceService.getDataDescMap();
        JSONArray valueTimeMap = sdsCommonService.getDicArray("ItemGetValTime");
        model.addAttribute("dicDescMap", dicDescMap);
        model.addAttribute("dataDescMap", dataDescMap);
        model.addAttribute("valueTimeMap",valueTimeMap);
        return PREFIX + "sdscusindexheader_compare.html";
    }

    @Override
    public List<SdsCusIndexHeaderEntity> page(Page<SdsCusIndexHeaderEntity> page, Map<String, Object> map) {
        return baseMapper.page(page,map);
    }


    @Override
    public List<SdsCusIndexHeaderEntity> list(Map<String, Object> map) {
        return baseMapper.list(map);
    }

    @Override
    public List<ZTreeNode> getIndexHeaderTree(Long entityId, Integer indexType) {
        List<ZTreeNode> tree = this.baseMapper.tree(entityId, indexType);
        return tree;
    }

    @Override
    public List<Map<String, Object>> selectIndexHeader(Map<String, Object> params) {
        return this.baseMapper.selectIndexHeader(params);
    }

    @Override
    public void importIndexHeader(List<List<Object>> indexAll, Long entityId) {

    }

    @Override
    public void importIndexItem(List<List<Object>> indexItemAll, Long entityId) {

    }

    @Override
    public List<SdsCusIndexHeaderEntity> getParentIndexHeader(Map<String, Object> map) {
        return null;
    }

    @Override
    public JSONObject getAnalysisCaseDic(Map<String, String> params) {
        return null;
    }

    @Override
    public String getTitleByField(Long entityId, Integer indexType, String field) {
        return null;
    }
}
